package com.jay.net.clazz.socket.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

class ConnectionHandler implements Runnable {
	DatagramSocket socket;
	DatagramPacket packet;

	ConnectionHandler(DatagramSocket socket, DatagramPacket packet) {
		this.socket = socket;
		this.packet = packet;
	}

	public void run() {
		handlePacket(socket, packet);
	}

	public void handlePacket(DatagramSocket socket, DatagramPacket packet) {
		try {
			byte[] buffer = packet.getData();
			int offset = packet.getOffset();
			int length = packet.getLength();
			// conversation not shown …
			// sets reply into buffer/offset/length
			packet.setData(buffer, offset, length);
			// write reply to the output
			socket.send(packet);
		} catch (IOException e) { /* ... */
		}
	} // handlePacket()
} // class
